/**
 * 
 */
package fhq.hcmute.edu.vn.adapter;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import fhq.hcmute.edu.vn.activities.R;

/**
 * @author Hien Nguyen Nov 10, 2012
 */
public class MySpinnerAdapter extends ArrayAdapter<String> {

	private List<String> objects;
	private Activity activity;

	public MySpinnerAdapter(Activity activity, List<String> objects) {
		super(activity, 0, objects);
		this.setObjects(objects);
		this.activity = activity;
	}

	public void add(String obj) {
		objects.add(obj);
	}

	public void add(int position, String obj) {
		objects.add(position, obj);
	}

	public List<String> getObjects() {
		return objects;
	}

	public void setObjects(List<String> objects) {
		this.objects = objects;
	}

	static class ViewHolder {
		private TextView txtSpinnerText;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			final ViewHolder holder = new ViewHolder();
			row = inflater.inflate(R.layout.spinner_item, null);
			holder.txtSpinnerText =
					(TextView) row.findViewById(R.id.txtSpinnerText);
			row.setTag(holder);

		}
		ViewHolder holder = (ViewHolder) row.getTag();
		holder.txtSpinnerText.setText(objects.get(position));
		return row;
	}
}
